
import {Next} from "koa";
import { AppCTX } from "../../types/koaExtend"
import { checkBrandAdmin } from "../../service/brandAdminService"
import Router from "koa-router";

const mw = async (ctx: AppCTX, next: Next) => {
  const reqUrl = ctx.request.url;
  console.log('brandAdminChecker =>', reqUrl)
  if (reqUrl.startsWith("/brd")) {
    const {brandId} = ctx.params
    const userId = ctx.userId
    const checkRet = await checkBrandAdmin(userId, brandId)
    if (!checkRet) {
      ctx.throw(403, "not authorized, user is not the brand admin")
    }
  }
  await next();
};

export default mw;

export const createBrandApiMWRouter = () => {
  const router = new Router();
  return router.use("/brd/:brandId/", mw)
}